var koa=require('koa')
var wechat=require('./wechat/g')
var weixin=require('./wx/reply')
var wx=require('./wx/index.js')


var app =new koa()

var ejs=require('koa-ejs')
var path=require('path');
var json = require('koa-json')

var index = require('./routes/index');

//初始化公众号菜单
var menu=require('./wx/menu')
var wechatApi=wx.getWechat()
wechatApi.deleteMenu().then(function(){
        return  wechatApi.createMenu(menu)
    }).then(function(msg){
            console.log(msg)
    })

//渲染
ejs(app, {
  root: path.resolve(__dirname, 'views'),
  layout: false,
  viewExt: 'ejs',
  cache: false,
  debug: false
});

app.use(require('koa-bodyparser')());
app.use(json());

app.use(require('koa-static')(__dirname + '/public'));

//用于接收微信服务器消息中间件
app.use(function *(next){
        //注意 接口配置信息，填写的URL 也需要怎加/wx  比如http://suzhidong.qicp.vip/wx  用于接收微信服务器发送的GET 或者 POST 请求
        if (this.url.indexOf('/wx')>-1) {
                console.log('微信')
                this.wxinit= wechat(wx.config.wechat,weixin.reply)
                yield this.wxinit(next)
        } 
        yield next
   })

app.use(index.routes(), index.allowedMethods());

app.listen(1234)

console.log('服务器启动')